﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using ZX.BLL.Extend;
using ZX.CRM.Bussiness;
using ZX.CRM.Library;
using ZX.EHR.Library;
using ZX.Web.UI.Controls;
using Newtonsoft.Json;

namespace ZX.WebApp.Crm.Report
{
    public partial class ListCustomerReport : ListPage
    {
        private string ActionStr = string.Empty;
        private BTCrmCustomer bll = new BTCrmCustomer();
        private string strNoData = "";
        
        protected void Page_Load(object sender, EventArgs e)
        {
            ActionStr = Request["Action"];
            if (ActionStr != null)
            {
                switch (ActionStr.ToLower())
                {
                    case "inittree":
                        InitTree();
                        break;
                    case "loadtree":
                        LoadTree();
                        break;
                }
            }
            if (!IsPostBack)
            {
                BaseBussnies deptUserPost = new BaseBussnies();
                deptUserPost.entity = new V_DatUserPostDept();
                IList<V_DatUserPostDept> list = (List<V_DatUserPostDept>)deptUserPost.GetEntitys<V_DatUserPostDept>("Userid='" + this.UserId.ToString() + "'");

                if (list == null || list.Count <= 0)
                    strNoData = "0";
            }
        }

        protected void InitTree()
        {
            BaseBussnies deptUserPost = new BaseBussnies();
            deptUserPost.entity = new V_DatUserPostDept();
            IList<V_DatUserPostDept> list = (List<V_DatUserPostDept>)deptUserPost.GetEntitys<V_DatUserPostDept>("Userid='" + this.UserId.ToString() + "'");
            if (list != null && list.Count > 0)
            {
                List<ZtreeHelper> listTree = new List<ZtreeHelper>();
                foreach (V_DatUserPostDept dpd in list)
                {
                    ZtreeHelper zh = new ZtreeHelper
                    {
                        id = dpd.DeptID.ToString(),
                        pId = "001",
                        name = dpd.DeptName,
                        isParent = true,
                        open = true,
                        iconOpen = "../../Scripts/JQuery_zTree_v3.3/css/zTreeStyle/img/diy/3.png",
                        iconClose = "",
                        click = true,
                        key1 = "dept"
                    };
                    listTree.Add(zh);
                }

                string json = string.Empty;
                if (list != null)
                    json = JsonConvert.SerializeObject(listTree);
                Response.Clear();
                Response.ContentType = "application/json";
                Response.Write(json);
                Response.End();
            }
            else
            {
                ZtreeHelper zh = new ZtreeHelper
                {
                    error = "true"
                };

                List<ZtreeHelper> listError = new List<ZtreeHelper>();
                listError.Add(zh);

                string json = string.Empty;
                json = JsonConvert.SerializeObject(listError);
                Response.Clear();
                Response.ContentType = "application/json";
                Response.Write(json);
                Response.End();
            }
        }

        protected void LoadTree()
        {
            string parentid = Request["id"];
            BaseBussnies deptDept = new BaseBussnies();
            deptDept.entity = new DatDepartment();
            IList<DatDepartment> list = (List<DatDepartment>)deptDept.GetEntitys<DatDepartment>("ParentID='" + parentid + "'");
            List<ZtreeHelper> listTree = new List<ZtreeHelper>();
            if (list != null && list.Count > 0)
            {
                //加载树节点的子节点
                foreach (DatDepartment dpd in list)
                {
                    ZtreeHelper zh = new ZtreeHelper
                    {
                        id = dpd.DeptID.ToString(),
                        pId = parentid,
                        name = dpd.DeptName,
                        isParent = true,
                        open = true,
                        iconOpen = "../../Scripts/JQuery_zTree_v3.3/css/zTreeStyle/img/diy/3.png",
                        iconClose = "",
                        click = true,
                        key1 = "dept"
                    };
                    listTree.Add(zh);
                }
            }

            //加载人员
            BaseBussnies deptUserPost = new BaseBussnies();
            deptUserPost.entity = new V_UserEmp();
            IList<V_UserEmp> IUserEmp = deptUserPost.GetEntitys<V_UserEmp>("deptid='" + parentid + "' and IsExpire=0");
            if (IUserEmp != null && IUserEmp.Count > 0)
            {
                for (int i = 0; i < IUserEmp.Count; i++)
                {
                    ZtreeHelper zh = new ZtreeHelper
                    {
                        id = IUserEmp[i].UserID.ToString(),
                        pId = parentid,
                        name = IUserEmp[i].Name,
                        isParent = false,
                        open = false,
                        icon = "../../Scripts/JQuery_zTree_v3.3/css/zTreeStyle/img/diy/userclose.gif",
                        click = true,
                        key1 = "user"
                    };
                    listTree.Add(zh);
                }
            }

            string json = string.Empty;
            if (listTree != null)
                json = JsonConvert.SerializeObject(listTree);
            Response.Clear();
            Response.ContentType = "application/json";
            Response.Write(json);
            Response.End();
        }

        public string ReturnNoData
        { get { return strNoData; } }
    }


}

